import type { SoElements } from '../../../../../../../../types/constants/personalization/so'
import { ThemeVarService } from '../../../../../../../../lib/abstract/style/theme_var/service'
import {
  AppStyleCollections,
  generateHideSelector,
  PAGE_ELEMENTS,
  SUPPORTED_PLATFORMS,
} from '../../../../../../../../types/constants'
import { SearchCommonStyles } from '../../common'
import { SoCommonPage } from './common'

export class SoWenkuPage {
  elements: SoElements = PAGE_ELEMENTS[SUPPORTED_PLATFORMS.SO]
  private static instance: SoWenkuPage
  private readonly themeVarService: ThemeVarService = ThemeVarService.getInstance()
  private readonly searchCommonStyles: SearchCommonStyles = SearchCommonStyles.getInstance()
  private readonly commonPage: SoCommonPage = SoCommonPage.getInstance()

  /**
   * 获取单例实例
   */
  public static getInstance(): SoWenkuPage {
    if (!SoWenkuPage.instance) {
      SoWenkuPage.instance = new SoWenkuPage()
    }
    return SoWenkuPage.instance
  }

  init(): void {
  }

  /**
   * 无搜索结果样式
   */
  noSearchResult(): string {
    return ``
  }

  hideSearchStyles(): string {
    return `
    .so-search__input.has-sug {
      border-radius: 10px 0 0 10px !important;
      border-bottom: 2px solid #0fb264 !important;
    }
    
    .so-suggest {
      display: none !important;
    }
    `
  }

  getMainStyles(): string {
    return `
  /* 隐藏样式 */
  &:has(${generateHideSelector('.wenku-search__main .content-filter')}):has(${generateHideSelector('.wenku-search__main .wenku-search__main__tag-filter')}) {
    #app .wenku-search__main .wenku-search__resList {
      margin-top: ${this.themeVarService.search.resultItem.gap} !important;
    }
  }
  &:has(${generateHideSelector('.wenku-search__main .content-filter')}) {
    #app .wenku-search__main .wenku-search__main__tag-filter {
      margin-top: 10px !important;
    }
  }
  
  /* 搜索历史隐藏、搜索建议隐藏 */
  &[${AppStyleCollections.Search.attr.HIDE_HISTORY}="true"][${AppStyleCollections.Search.attr.HIDE_SUGGESTIONS}="true"] {
    form.so-search__form {
      ${this.hideSearchStyles()}
    }
  }
  /* 搜索历史隐藏、搜索建议显示 */
  &[${AppStyleCollections.Search.attr.HIDE_HISTORY}="true"]:not([${AppStyleCollections.Search.attr.HIDE_SUGGESTIONS}]) {
    /* 无搜索建议 */
    form.so-search__form[${AppStyleCollections.Search.attr.IS_SUGGESTIONS_EMPTY}="true"] {
      ${this.hideSearchStyles()}
    }
  }
  /* 搜索历史显示、搜索建议隐藏 */
  &:not([${AppStyleCollections.Search.attr.HIDE_HISTORY}])[${AppStyleCollections.Search.attr.HIDE_SUGGESTIONS}="true"] {
    /* 无搜索历史 */
    form.so-search__form[${AppStyleCollections.Search.attr.IS_HISTORY_EMPTY}="true"] {
      ${this.hideSearchStyles()}
    }
  }

  /* 主样式 */  
  #app {
    .so-header {
      background-color: var(${this.themeVarService.search.header.background}) !important;
      padding: 16px 20px !important;
      display: flex !important;
      justify-content: center !important;
      align-items: center !important;
      height: auto !important;
      
      &.fixed {
        box-shadow: none !important;
      }
      
      .so-header__content {
        position: unset !important;
        width: fit-content !important;
        min-width: unset !important;
        height: auto !important;
        
        .so-header__left {
          .so-suggest {
            .so-suggest__item {
              /* 搜索历史高亮 */
              &.history {
                .so-suggest__text {
                  color: var(${this.themeVarService.search.history.text}) !important;
                    
                  &:has(> b) {
                    color: var(${this.themeVarService.em.text}) !important;
                  }
                  
                  b {
                    color: var(${this.themeVarService.search.history.text}) !important;
                  }
                }
              }
              
              /* 搜索建议高亮 */
              &.suggest {
                .so-suggest__text {
                  color: var(${this.themeVarService.search.suggestions.text}) !important;
                  
                  &:has(> b) {
                    color: var(${this.themeVarService.em.text}) !important;
                  }
                }
              }
            }
          }
        }
        
        .so-header__right {
          padding-right: 20px !important;
          
          a, span {
            color: var(${this.themeVarService.search.default.text}) !important;
          }
          
          .vip-entrance__text, a, [data-so-mod=personal_centre] {
            &:hover {
              color: var(${this.themeVarService.link.default.text}) !important;
            }
          }
          
          .vip-entrance__discount-tip {
            color: white !important;
          }
        }
      }
    }
    
    /* 搜索分类 */
    .so-nav-container {
      ${this.commonPage.tabStyles()}
    }

    .wenku-search__main {
      padding-bottom: ${this.themeVarService.search.resultItem.gap} !important;

      .wenku-search__main__left {
        padding: 0 !important;
        margin: 0 auto !important;
        max-width: 100% !important;
        
        /* 内容筛选 */
        .content-filter {
          .wk-filter {
            .wk-filter__label {
              color: var(${this.themeVarService.search.default.text}) !important;
            }
            
            .wk-filter__list {
              li {
                &:not(.active) {
                  span {
                    color: var(${this.themeVarService.search.default.text}) !important;
                  }
                  
                  &:hover {
                    span {
                      color: var(${this.themeVarService.link.default.text}) !important;
                    }
                  }
                }
              }
            }
          }
        }
        
        /* 标签筛选 */
        .wenku-search__main__tag-filter {
          .tag-list {
            li {
              &:not(.active) {
                span {
                  color: var(${this.themeVarService.search.default.text}) !important;
                }
                
                &:hover {
                  span {
                    color: var(${this.themeVarService.link.default.text}) !important;
                  }
                }
                
                &::after {
                  background-color: var(${this.themeVarService.message.info.textPrimary}) !important;
                }
              }

              &.active {
                span {
                  color: var(${this.themeVarService.message.success.textPrimary}) !important;
                }
                
                &::after {
                  background-color: var(${this.themeVarService.message.success.textPrimary}) !important;
                }
              }
            }
          }
        }
        
        /* 广告列表 */
        #fengwu-ads {
          margin-top: 30px !important;
          margin-bottom: ${this.themeVarService.search.resultItem.gap} !important;
          
          > ul {
            ${this.searchCommonStyles.resultContainerStyles()}
            
            &::after {
              display: none !important;
            }
            
            > li {
              em {
                color: var(${this.themeVarService.em.text}) !important;
              }
              
              ${this.searchCommonStyles.resultItemStyles({ noMarginBottom: true })}
              
              /* 标题 */
              > a.e_haosou_fw_bg_title {
                color: var(${this.themeVarService.link.default.text}) !important;
                text-decoration: none !important;
                
                &:hover {
                  color: var(${this.themeVarService.link.default.textHover}) !important;
                  text-decoration: none !important;
                  
                  em {
                    color: var(${this.themeVarService.link.default.textHover}) !important;
                  }
                }
                
                em {
                  color: var(${this.themeVarService.link.default.text}) !important;
                }
              }
              
              p {
                color: var(${this.themeVarService.search.default.text}) !important;
                
                .inner_desc {
                  color: var(${this.themeVarService.search.default.text}) !important;
                }
              }
              
              a {
                color: var(${this.themeVarService.link.default.text}) !important;
              }
              
              .e_haosou_fw_cover {
                background-color: transparent !important;
              }
              
              .e_haoso_fengwu_extend {
                /* 广告提示 */
                .tips-triggr {
                  display: none !important;
                }
              }
            }
          }
        }
        
        /* 搜索结果列表 */
        .wenku-search__resList {
          ${this.searchCommonStyles.resultContainerStyles()}
            
          /* 搜索结果项 */  
          > li {
            ${this.searchCommonStyles.resultItemStyles({ noMarginBottom: true })}
            
            /* 广告项 */
            &.fengwu-biz {
              .fengwu-biz__title {
                color: var(${this.themeVarService.link.default.text}) !important;
                text-decoration: none !important;
                
                em {
                  color: var(${this.themeVarService.link.default.text}) !important;
                }
                
                &:hover {
                  color: var(${this.themeVarService.link.default.textHover}) !important;
                  text-decoration: none !important;
                  
                  em {
                    color: var(${this.themeVarService.link.default.textHover}) !important;
                  }
                }
              }
              
              .fengwu-biz__info-desc {
                color: var(${this.themeVarService.search.default.text}) !important;
                
                em {
                  color: var(${this.themeVarService.search.default.text}) !important;
                }
              }
              
              .fengwu-biz__info-cite, .fengwu-biz__info-tip {
                display: none !important;
              }
            }
            
            /* 标题 */
            .wenku-search__resList__item__title {
              width: 100% !important;
              ${this.searchCommonStyles.titleStyles('a span', 'b')}
              
              .wenku-search__resList__item__vipIcon {
                position: absolute !important;
                right: 10px !important;
              }
            }
            
            /* 描述 */
            .wenku-search__resList__item__info {
              width: 100% !important;

              .wenku-search__resList__item__info__desc {
                color: var(${this.themeVarService.search.default.text}) !important;
              }
            }
          }
        }
        
        /* 相关搜索 */
        .wenku-search__relatedQuery {
          .wenku-search__relatedQuery__title {
            color: var(${this.themeVarService.search.default.text}) !important;
          }
        }
      }
      
      .wenku-search__main__right {
        position: absolute !important;
        right: 100px !important;
        
        .wenku__relatedDoc__title {
          color: var(${this.themeVarService.search.default.text}) !important;
        }
        
        .wenku__relatedDoc__list {
          a {
            span {
              color: var(${this.themeVarService.link.default.text}) !important;
              
              &:hover {
                color: var(${this.themeVarService.link.default.textHover}) !important;
                text-decoration: none !important;
              }
            }
          }
        }
      }
    }
    
    /* 页脚 */
    ${this.commonPage.footStyles()}
    .wenku-search__footer-wrap {
      .wenku-page__footer {
        margin: 0 auto !important;
      }
    }
  }  
    `
  }
}
